package com.wanghaixin.controller;

import com.wanghaixin.dto.ActivityDTO;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.Car;
import com.wanghaixin.pojo.CarMake;
import com.wanghaixin.service.ActivityService;
import com.wanghaixin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/3 14:29
 * @Description: 活动管理模块
 */
@Controller
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date: 14:31  2020/11/3
     * @Version:  1.0.0
     * @Description:  分页查询
     */
    @RequestMapping("/sys/activity/list")
    //不添加下面注解会报一下错误
    //Error resolving template [sys/make/list], template might not exist or might not be accessible by any of the configured Template Resolvers
    @ResponseBody
    public DataGridResult findByPage(QueryDTO queryDTO){
        return activityService.findByPage(queryDTO);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  14:31  2020/11/3
     * @Version:  1.0.0
     * @Description:  删除
     */
    @RequestMapping("/sys/activity/del")
    @ResponseBody
    public R delActivity(@RequestBody List<Long> ids){
        for (Long id : ids) {
            activityService.delActivity(id);
        }
        return R.ok();
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date: 14:31  2020/11/3
     * @Version:  1.0.0
     * @Description:  id查询
     */
    @RequestMapping("/sys/activity/info/{id}")
    @ResponseBody
    public R findById(@PathVariable("id") Long id){
        ActivityDTO byId = activityService.findById(id);
        return R.ok().put("activity",byId);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  14:31  2020/11/3
     * @Version:  1.0.0
     * @Description:  添加方法
     */
    @RequestMapping("/sys/activity/save")
    @ResponseBody
    public R addActivity(@RequestBody ActivityDTO activityDTO){
        int i = activityService.addActivity(activityDTO);
        return i>0 ? R.ok():R.error("添加失败");
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  14:31  2020/11/3
     * @Version:  1.0.0
     * @Description:  修改方法
     */
    @RequestMapping("/sys/activity/update")
    @ResponseBody
    public R upActivity(@RequestBody ActivityDTO activityDTO){
        int i = activityService.updateActivity(activityDTO);
        return i>0 ? R.ok():R.error("修改失败");
    }
}
